package com.petalsgroup.petals.web.servlet.admin;

import com.petalsgroup.petals.pojo.AnimationDetailedDO;
import com.petalsgroup.petals.pojo.AnimationDetailedVO;
import com.petalsgroup.petals.pojo.AnimationSimpleDO;
import com.petalsgroup.petals.pojo.CommentsDO;
import com.petalsgroup.petals.service.admin.AdminService;
import com.petalsgroup.petals.service.admin.CommentAdminService;
import com.petalsgroup.petals.service.admin.UserAdminService;
import com.petalsgroup.petals.service.admin.impl.AdminServiceIpml;
import com.petalsgroup.petals.service.admin.impl.CommentAdminServiceIpml;
import com.petalsgroup.petals.service.admin.impl.UserAdminServiceIpml;
import com.petalsgroup.petals.service.animation.AnimationService;
import com.petalsgroup.petals.service.animation.impl.AnimationServiceImpl;
import com.petalsgroup.petals.util.LimitUtil;
import com.petalsgroup.petals.util.RecentlyUtil;
import com.petalsgroup.petals.web.BaseServlet;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.UUID;

@WebServlet("/commentAdmin/*")
public class CommentManagementServlet extends BaseServlet {
    CommentAdminService cas = new CommentAdminServiceIpml();
    Object pageFlag =null;
    /**
     * 尝试在评论管理模块获取已经登陆的用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object admin = req.getSession().getAttribute("loginAdmin");
        writeValue(resp,admin);
    }

    /**
     * 获取评论信息list
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void commentList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        req.getSession().setAttribute("pageFlag","commentListFlag");

        LimitUtil commentAdminLimit = new LimitUtil();
        req.setAttribute("commentAdminLimit", commentAdminLimit);
        //List<CommentsDO> list = as.findAllComment();
        List<CommentsDO> list = cas.findAllCommentByPages(commentAdminLimit);
        req.setAttribute("list",list);

        req.getRequestDispatcher("/WEB-INF/admin_view/commentAdmin.jsp").forward(req, resp);
    }
    /**
     * 分页获取评论信息list
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void commentAdminPageJump(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        req.setAttribute("barid","commentAdmin");
        LimitUtil commentAdminLimit = new LimitUtil(Long.valueOf(req.getParameter("total")),
                Integer.valueOf(req.getParameter("num")),
                Integer.valueOf(req.getParameter("currentPage")));
        //List<UserDO> list = as.findAll();
        req.setAttribute("commentAdminLimit", commentAdminLimit);
        List<CommentsDO> list = cas.findAllCommentByPages(commentAdminLimit);
        req.setAttribute("list",list);
        req.getRequestDispatcher("/WEB-INF/admin_view/commentAdmin.jsp").forward(req, resp);
    }
    /**
     * 删除评论
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void deleteComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数 - id
        String com_id = req.getParameter("com_id");
        //2.调用service的方法 - 删除
        cas.deleteComment(com_id);
        pageFlag=req.getSession().getAttribute("pageFlag");
        if ("findCommentByTimeQuantumFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/findCommentByTimeQuantum").forward(req,resp);
        }else if ("commentListFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/commentAdminPageJump").forward(req,resp);
        }else if ("findCommentByUserIdFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/findCommentByUserId").forward(req,resp);
        }else if ("findCommentByAdminIdFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/findCommentByAdminId").forward(req,resp);
        }else if ("findTopCommentByLikeNumFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/findTopCommentByLikeNum").forward(req,resp);
        }
        //resp.sendRedirect(req.getContextPath() + "/admin/commentAdmin");
    }
    /**
     * 注销选中的评论
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void deleteSelectComment(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取所有com_ids所对应的value
        String[] com_ids = req.getParameterValues("com_id");
        //System.out.println("进入方法");
        //2.调用service方法禁言
        cas.deleteSelectComment(com_ids);
        //3.跳转到userList - 重定向
        pageFlag=req.getSession().getAttribute("pageFlag");
        if ("findCommentByTimeQuantumFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/findCommentByTimeQuantum").forward(req,resp);
        }else if ("commentListFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/commentAdminPageJump").forward(req,resp);
        }else if ("findCommentByUserIdFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/findCommentByUserId").forward(req,resp);
        }else if ("findCommentByAdminIdFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/findCommentByAdminId").forward(req,resp);
        }else if ("findTopCommentByLikeNumFlag"==pageFlag){
            req.getRequestDispatcher("/commentAdmin/findTopCommentByLikeNum").forward(req,resp);
        }
    }
    /**
     * 根据时间段找
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findCommentByTimeQuantum(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String timeQuantum = req.getParameter("timeQuantum");
        req.setAttribute("barid","commentAdmin");
        req.getSession().setAttribute("pageFlag","findCommentByTimeQuantumFlag");
        //2.调用service方法禁言
        List<CommentsDO> list=cas.findCommentByTimeQuantum(timeQuantum);
        //System.out.println("获取回来的数据"+list.size());
        //System.out.println(list.get(0).toString());
        req.setAttribute("list",list);
        req.setAttribute("timeQuantum",timeQuantum);
        //3.跳转到userList - 转发
        req.getRequestDispatcher("/WEB-INF/admin_view/commentAdmin.jsp").forward(req, resp);
    }
    /**
     * 根据用户id查询评论
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findCommentByUserId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userInfo");
        req.setAttribute("barid","commentAdmin");
        req.getSession().setAttribute("pageFlag","findCommentByUserIdFlag");
        //2.调用service方法禁言
        List<CommentsDO> list=cas.findCommentByUserId(userId);
        req.setAttribute("list",list);
        req.setAttribute("userInfo",userId);
        //3.跳转到userList - 转发
        //resp.sendRedirect(req.getContextPath() + "/admin/userList");
        req.getRequestDispatcher("/WEB-INF/admin_view/commentAdmin.jsp").forward(req, resp);
    }
    /**
     * 根据动漫id查询评论
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findCommentByAdminId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String adminId = req.getParameter("userInfo");
        req.setAttribute("barid","commentAdmin");
        req.getSession().setAttribute("pageFlag","findCommentByAdminIdFlag");
        List<CommentsDO> list=cas.findCommentByAdminId(adminId);
        req.setAttribute("list",list);
        req.setAttribute("userInfo",adminId);
        //跳转到commentAdmin - 转发
        //resp.sendRedirect(req.getContextPath() + "/admin/userList");
        req.getRequestDispatcher("/WEB-INF/admin_view/commentAdmin.jsp").forward(req, resp);
    }

    /**
     * 查询点赞最高的N条评论
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author wgd
     */
    public void findTopCommentByLikeNum(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String number = req.getParameter("userInfo");
        req.setAttribute("barid","commentAdmin");
        req.getSession().setAttribute("pageFlag","findTopCommentByLikeNumFlag");
        List<CommentsDO> list=cas.findTopCommentByLikeNum(number);
        req.setAttribute("list",list);
        req.setAttribute("userInfo",number);
        //跳转到commentAdmin - 转发
        //resp.sendRedirect(req.getContextPath() + "/admin/userList");
        req.getRequestDispatcher("/WEB-INF/admin_view/commentAdmin.jsp").forward(req, resp);
    }
}
